//
//  JJHUD.swift
//  JJToolSwift
//
//  Created by heihei on 2024/5/11.
//

import Foundation
import MBProgressHUD

/// 弹窗动画文件类型
enum JJHUDType: Int {
    /// 普通菊花类型
    case normal
    /// 洛丽塔JSON动画
    case lottie
    /// gif动画
    case gif
}

/// 弹窗状态类型
public enum JJHUDStatus {
    /// 成功 ✅
    case success
    /// 失败 😭
    case failure
    /// 错误 ❎
    case error
    /// 警告 ⚠️
    case warn
    /// 微笑 😁
    case smile
    /// 带图片信息提示 ✉️
    case messagePict
    /// 不带图片信息提示
    case messageNoPict
    /// 等待，不会自动消失，需要手动隐藏
    case waiting
}

/// 带图片的提示框，默认显示在window上
public class JJHUD: MBProgressHUD {
    /// 是否正在显示
    private var isShowing: Bool = false
    
    public static let shared = JJHUD(view: (UIApplication.shared.keyWindow ?? UIViewController.current()?.view) ?? UIView())
    
    public override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    // 初始化
    override private init(view: UIView) {
        super.init(view: view)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    public override func copy() -> Any {
        return self
    }
    public override func mutableCopy() -> Any {
        return self
    }
    // Optional
    func reset() {
        JJLog("重置单例")
        // Reset all properties to default value
    }
    
    
    /// 在 window 上添加一个 HUD
    /// - Parameters:
    ///   - status: hud状态类型
    ///   - text: 文字
    public static func show(status: JJHUDStatus = .messageNoPict, text: String = "") {
        DispatchQueue.main.async {
            let hud = JJHUD.shared
            hud.isShowing = true
            hud.defaultConfig(status: status, text: text)
            UIApplication.shared.keyWindow?.addSubview(hud)
            // 设置图片
            switch status {
            case .success:
                hud.loadPicture(imageName: "success") {
                    JJHUD.hiddeHUD()
                }
            case .failure:
                hud.loadPicture(imageName: "frustrated") {
                    JJHUD.hiddeHUD()
                }
            case .smile:
                hud.loadPicture(imageName: "smile") {
                    JJHUD.hiddeHUD()
                }
            case .error:
                hud.loadPicture(imageName: "error") {
                    JJHUD.hiddeHUD()
                }
            case .waiting:
                hud.loadWaitingAnimated(text: text)
            case .warn:
                hud.loadPicture(imageName: "warn") {
                    JJHUD.hiddeHUD()
                }
            case .messagePict:
                hud.loadPicture(imageName: "message") {
                    JJHUD.hiddeHUD()
                }
            case .messageNoPict:
                hud.loadMessage(text: text) {
                    JJHUD.hiddeHUD()
                }
            }
        }
    }
    
    /// 隐藏hud
    public static func hiddeHUD() {
        JJHUD.shared.isShowing = false
        JJHUD.shared.hide(animated: true)
    }
}
